const app = getApp();
let tool = require('../../../utils/tool.js');
Page({
  data: {
    innerAudioContext: new Object(),
    wordList: {},
    wordCss: {},
    wordLen: 0,
    studyLen: 0,
    rtype: 3,
    saveName: 'lword3',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let rtype1 = JSON.parse(options.type);
    console.log(rtype1);
    // if(rtype1!=""){
    this.setData({
      rtype: rtype1,
      saveName: 'lword' + rtype1
    })
    // }

    try {
      let value = wx.getStorageSync(this.data.saveName)
      if (value) {
        console.log(JSON.parse(value));
        let tmparr = JSON.parse(value);
        for (let i = 0; i < (tmparr).length; i++) {
          tmparr[i].dis = 'none';
          tmparr[i].click = 0;
          tmparr[i].fontw = 'normal'
        }
        this.setData({
          wordList: tmparr,
          wordLen: tmparr.length,
        });
      } else {
        this.getData();
      }
    } catch (e) {
      console.log(e);
      this.getData();
    }



    //音频插件处理
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    this.data.innerAudioContext.src = app.globalData.mp3.selected;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }, //从接口获取数据
  getData: function() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let paramArr = {
      // access_token:app.globalData.access_token,
      type: that.data.rtype,
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
    }
    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let sign = rs.sign;
    let apiUrl = app.globalData.apiURL + "review/readerr?sign=" + sign;
    let classPro = new Promise(
      function(resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {
            console.log(res);
            resolve(res);
          },
          complete: function () {
            wx.hideLoading();
          }
        })
      }

    );


    classPro.then(function(res) {
      if (res.data.code === 1) {
        console.log(res);
        console.log(res.data.data)
        let tmparr = res.data.data;
        for (let i = 0; i < (res.data.data).length; i++) {
          tmparr[i].dis = 'none';
          tmparr[i].click = 0;
          tmparr[i].fontw = 'normal'
        }
        that.setData({
          wordList: tmparr,
          wordLen: (res.data.data).length,

          // wordLen: res.data.data.word[0].length,
          // wordCur: res.data.data.word[0][0],
          // allWord:res.data.data.word[0][0].word
        });
        wx.setStorage({
          key: that.data.saveName,
          data: JSON.stringify(res.data.data)
        })
      }
    })
  },
  clickWorld: function(e) {
    let tmp = new Object;
    tmp = this.data.wordList;
    let hasClick = this.data.studyLen;
    let idx = e.currentTarget.dataset.idx;
    this.data.innerAudioContext.src = tmp[idx].vedio_url;
    this.data.innerAudioContext.play();
    if (tmp[idx].click == 0) {
      tmp[idx].fontw = 800;
    } else if (tmp[idx].click == 1) {
      tmp[idx].bgcolor = "#D6FFE7";
      tmp[idx].dis = 'flex';
      hasClick++;
    } else if (tmp[idx].click == 2) {

      if (tmp[idx].dis == 'none') {
        tmp[idx].dis = 'flex';
      } else {
        tmp[idx].dis = 'none';
      }

    }
    if (tmp[idx].click < 2) {
      tmp[idx].click = parseInt(tmp[idx].click) + 1;
    }
    this.setData({
      wordList: tmp,
      studyLen: hasClick
    });
  },
  goTo: function() {
    if (parseInt(this.data.studyLen) != parseInt(this.data.wordLen)) {
      wx.showToast({
        title: '您还有未查看的的单词',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let tid = this.data.rtype;
    wx.removeStorage({
      key: this.data.saveName,
      success(res) {
        console.log(res);
        if (tid != 0) {
          // wx.switchTab({
          //     url: '../../review/review',
          // })
          wx.navigateBack({
            delta: -1
          })
        } else {
          wx.navigateTo({
            url: '../lworldReport/lworldReport?type=0',
          });
        }

      }
    })

  }
})